<?php
use Phalcon\DI\FactoryDefault\CLI as CliDI;  
use Phalcon\CLI\Console as ConsoleApp; 
use Phalcon\Logger\Adapter\File as FileAdapter;


function _p($val, $die = true)
{
     header("content-Type: text/html; charset=Utf-8");  
    $out  = "<pre style=\"background: #000; color: #ccc; font: 12px 'fixedsys'; text-align: left; width: 100%; padding: 5px\">\n";
    $out .= print_r($val, true);
    $out .= "</pre>\n";
    echo $out;
    if ($die) {
       exit();
    }

   
}
define('VERSION', '1.0.0');
/*
 * 获取文件的更新
 */
function last_modified($app_id){
    $info_arr = array();
    foreach($this->detect($app_id) as $item){
        //$modified = max($modified,filemtime($this->getPathname()));
        //todo: md5
        $filename = $this->getPathname();
        if(is_dir($filename)){
            foreach(utils::tree($filename) AS $k=>$v){
                                    if (is_dir($v)) continue;
                $info_arr[$v] = md5_file($v);
            }
        }else{
            $info_arr[$filename] = md5_file($filename);
        }
    }
    ksort($info_arr);
    return md5(serialize($info_arr));
}
/*
 * 获取文件的更新
 */
function last_modified_file($filename){
    $info_arr = array();
    if(is_file($filename)){
       $info_arr[$filename] = md5_file($filename);
    }
    ksort($info_arr);
    return md5(serialize($info_arr));
}    
    
/*
 * 初始化加载函数
 */
function single($class_name,$arg=null){
    
    $new_class_name = str_replace('_', '\\', $class_name);
     //var_dump($new_class_name);exit();
    return new $new_class_name($arg);
 }    




// 使用CLI工厂类作为默认的服务容器
$di = new CliDI();

// 定义应用目录路径
defined('APPLICATION_PATH')|| define('APPLICATION_PATH', realpath(dirname(__FILE__)));
define('APP_PATH',dirname(dirname(APPLICATION_PATH)));
define('DATA_DIR',APP_PATH."/data");

/*
 * 写日志函数
 */
function _Log($message){
    $path = DATA_DIR.'/logs/debug.log';
    //_p($path);
    $logger = new FileAdapter($path);
    $logger->log(print_r($message, true)); 
}

/**
 * 注册类自动加载器
 */
$loader = new Phalcon\Loader();
//注册命名空间
$loader->registerNamespaces(
    array(
       'Base\Lib' =>  APPLICATION_PATH . '/Lib',
       'Base\Lib\Application' =>  APPLICATION_PATH . '/Lib/Application',
       'Base\Lib\Kvstore' =>  APPLICATION_PATH . '/Lib/Kvstore',

    )
)->register();

$loader->registerDirs(
    array(
        APPLICATION_PATH . '/tasks',
        APPLICATION_PATH . '/Lib',
        APPLICATION_PATH . '/Lib/Application/',
        APPLICATION_PATH . '/Lib/Kvstore/',
        
    )
);
//$loader->register();

//注册类名
$loader->registerClasses(
    array(
        "DBTable"         => APPLICATION_PATH . '/Lib/Application/DBTable.php',
    )
);
// register autoloader
$loader->register();
//_p($loader);
// 加载配置文件（如果存在）
if (is_readable(APP_PATH . '/config/config.php')) {
    $config = include APP_PATH. '/config/config.php';
    $di->set('config', $config);
}
global $_CONFIG;
$_CONFIG = $config;

//_p($di);
// 创建console应用
$console = new ConsoleApp();
$console->setDI($di);

/**
 * 处理console应用参数
 */
$arguments = array();
if(isset($argv)){
    foreach ($argv as $k => $arg) {
        if ($k == 1) {
            $arguments['task'] = $arg;
        } elseif ($k == 2) {
            $arguments['action'] = $arg;
        } elseif ($k >= 3) {
            $arguments['params'][] = $arg;
        }
    }  
}


// 定义全局的参数， 设定当前任务及动作
define('CURRENT_TASK',   (isset($argv[1]) ? $argv[1] : null));
define('CURRENT_ACTION', (isset($argv[2]) ? $argv[2] : null));

try {
    // 处理参数
    $console->handle($arguments);
} catch (PhalconException $e) {
    echo $e->getMessage();
    exit(255);
}